package com.bt.controller;

import com.bt.entity.User;
import com.bt.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public String register(User user,Model model) {
        // 1. 检查用户名是否已存在
        if (userService.findByUsername(user.getName()) != null) {
            model.addAttribute("msg", "用户名已被占用");
            return "register";
        }
        userService.saveUser(user);
        // 2. 注册成功，跳转到登录页
        return "redirect:/login";
    }
    //改造原来的查询方法
    @GetMapping("/ulist")
    public String list(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "sex", required = false) String sex,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Model model) {
        // 调用分页查询
        PageInfo<User> pageInfo = userService.findByCondition(name, sex, address, pageNum, pageSize);
        // 将结果传递给前端
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name",name);
        model.addAttribute("sex",sex);
        model.addAttribute("address",address);
        return "ulist";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        // 清除用户信息
        session.invalidate();  // 使会话无效，移除会话中的所有属性
        return "redirect:/login";  // 跳转到登录页
    }
    @PostMapping("/dologin")
    public String login(@RequestParam(value = "name") String name,
                        @RequestParam(value = "password") String password,
                        Map<String, String> maps, HttpSession session) {
        User user = userService.login(name, password);
        if (user != null) {
            session.setAttribute("user", user);
            return "redirect:/main";

        } else {
            maps.put("msg", "用户名或密码错误");
            return "login";
        }
    }
  /*//查询所有用户
    @GetMapping("/ulist")
    public String getList(Model model){
        List<User> ulist=userService.getUserList();
        model.addAttribute("ulist",ulist); //将查询出来的数据放入model对象中
        return "ulist";
    }*/
    //查询单个用户
    @GetMapping("/user/{id}")
    public String selectById(@PathVariable("id") Integer id, Model model){
        User user=userService.selectUserById(id);
        model.addAttribute("user",user);
        return "detail";
    }
    //删除用户
    @RequestMapping("/delUser/{id}")
    public String delUser(@PathVariable("id") Integer id){
        userService.delUser(id);
        return "redirect:/ulist";
    }
    //去修改页面
    @GetMapping("/toUpdateUser/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){
        User user=userService.selectUserById(id);
        model.addAttribute("user",user);
        return "update";
    }
    //修改用户
    @PostMapping("/updateUser")
    public String updateUser(User user) {
        userService.updateUser(user);
        return "redirect:/ulist";
    }
    //增加用户
    @PostMapping("/addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "redirect:/ulist";
    }
}
